package com.google.coffeeshop.gui.desktop.orderpayment;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import vn.bang.utility.LongUtil;

import com.google.coffeeshop.business.MaintenanceBusiness;
import com.google.coffeeshop.business.OrderBusiness;
import com.google.coffeeshop.entity.SeatDTO;
import com.google.coffeeshop.gui.desktop.AbstractBaseFrame;
import com.google.coffeeshop.gui.desktop.SwingFactory;
import com.google.coffeeshop.gui.desktop.orderpayment.seatdetail.SeatDetailFrame;
import com.google.coffeeshop.gui.desktop.selfcomponent.LRItem;
import com.google.coffeeshop.gui.desktop.selfcomponent.MultiColumnsComboBox;

public class SeatTransferingFrame extends AbstractBaseFrame {
	private SeatDetailFrame frmSeatDetail;
	private OrderPaymentPanel pnlOrderPayment;
	
	private List<SeatDTO> seatOptions = OrderBusiness.findAllSeats();
	
	public SeatTransferingFrame(SeatDetailFrame frmSeatDetail) {
		super("Seat Transfering");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.frmSeatDetail = frmSeatDetail;
		mtcbbSeatSource.setEditable(Boolean.FALSE);
		selectItemComboBox(mtcbbSeatSource, frmSeatDetail.getSeat().getId());
		init();
	}

	public SeatTransferingFrame(OrderPaymentPanel pnlOrderPayment) {
		super("Seat Transfering");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.pnlOrderPayment = pnlOrderPayment;
		
		init();
	}
	
	private void selectItemComboBox(MultiColumnsComboBox mtcbbSeatSource, long seatId) {
		if (seatOptions != null && !seatOptions.isEmpty()){
			for (SeatDTO seat : seatOptions) {
				if (seat.getId() != null && seat.getId() == seatId) {
					mtcbbSeatSource.setSelectedItem(seat);
				}
			}
		}
	}
	
	private void init() {
		setSize(300, 200);
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 5, 0);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 0;
		gbc_comboBox.gridy = 0;
		getContentPane().add(mtcbbSeatSource, gbc_comboBox);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(SeatTransferingFrame.class.getResource("/com/google/coffeeshop/gui/desktop/icon/size48/transfer-48.png")));
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.insets = new Insets(0, 0, 5, 0);
		gbc_label.gridx = 0;
		gbc_label.gridy = 1;
		getContentPane().add(label, gbc_label);
		
		GridBagConstraints gbc_comboBox_1 = new GridBagConstraints();
		gbc_comboBox_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox_1.gridx = 0;
		gbc_comboBox_1.gridy = 2;
		getContentPane().add(mtcbbSeatDestination, gbc_comboBox_1);
		
		JButton btnOK = new JButton("OK");
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LRItem item1 = (LRItem)mtcbbSeatSource.getSelectedItem();
				LRItem item2 = (LRItem)mtcbbSeatDestination.getSelectedItem();
				MaintenanceBusiness.transferSeatToSeat(LongUtil.parseLong(item1.getRightText()), LongUtil.parseLong(item2.getRightText()));
				refreshParentGUI();
				disposeFrame();
				disposeSeatDetailFrame();
				SwingFactory.showMessageDiaglogInform("Transfering successfully!");
			}
		});
		GridBagConstraints gbc_button_ok = new GridBagConstraints();
		gbc_button_ok.fill = GridBagConstraints.HORIZONTAL;
		gbc_button_ok.gridx = 0;
		gbc_button_ok.gridy = 3;
		getContentPane().add(btnOK, gbc_button_ok);
		
		addDatas();
	}
	
	private void disposeSeatDetailFrame() {
		if (frmSeatDetail != null) {
			frmSeatDetail.disposeFrame();
		}
	}
	
	public void refreshParentGUI() {
		if (frmSeatDetail != null) {
			frmSeatDetail.refreshGUI();
			frmSeatDetail.refreshParentGUI();
		}
		if (pnlOrderPayment != null) {
			pnlOrderPayment.refreshGUI();
		}
	}
	
	
	
	private MultiColumnsComboBox mtcbbSeatSource = SwingFactory.createMultiColumnsComboBox();
	private MultiColumnsComboBox mtcbbSeatDestination = SwingFactory.createMultiColumnsComboBox();
	
	private void addDatas() {
		
		addDataMultiComboBox(mtcbbSeatSource, seatOptions);
		addDataMultiComboBox(mtcbbSeatDestination, seatOptions);
	}
	
	@SuppressWarnings("unchecked")
	private void addDataMultiComboBox(MultiColumnsComboBox multiColumnsComboBox, List<SeatDTO> seats) {
		if (seats != null && !seats.isEmpty()) {
			DefaultComboBoxModel<LRItem> model = new DefaultComboBoxModel<LRItem>();
			for (SeatDTO seat : seats) {
				model.addElement(new LRItem(seat.getCode(), String.valueOf(seat.getSeatName())));
			}
			multiColumnsComboBox.setModel(model);
		}
	}
	
	private static final long serialVersionUID = 1L;

}
